<?php

class Commdata_Region_Module extends CI_Module {

	private $_data = array();

	public function __construct() {
		parent::__construct();
		$this->load->model('region_model', 'region');
		$this->_data['firstoption'] = '<option value="">区、县级市</option>';
	}

	public function getCountry($provinceid = '', $cityid = '', $regionid = '',$count = 3)
	{
		$this->_data['provincelist'] = $this->getProvince($provinceid,'return');
		$this->_data['citylist'] = $this->getCity($provinceid, $cityid, 'return');
		$this->_data['regionlist'] = $this->getRegion($cityid, $regionid, 'return');

		$this->_data['provinceid']	= $provinceid;
		$this->_data['cityid']		= $cityid;
		$this->_data['regionid']	= $regionid;
		$this->_data['count'] = $count;
		$this->load->view("region", $this->_data);
	}


	// 获取省份数据
	public function getProvince($provinceid = '', $rettype='html')
	{
		$retdata = array();
		$data = $this->region->getProvince($provinceid);

		$retdata = '<option value="">省份</option>' . array2option($data, $provinceid);

		if ($rettype == 'html')
		{
			exit($retdata);
		}
		else if ($rettype == 'json')
		{
			$ret['code'] = '1';
			$ret['msg'] = $retdata;
			exit(json_encode($ret));
		}
		else if ($rettype == 'return')
		{
			return $retdata;
		}
	}

	// 获取城市数据
	public function getCity($provinceid, $cityid = '', $rettype = 'html')
	{
		if (empty($provinceid)) return '<option value="">地级市</option>';

		$retdata = array();
		$data = $this->region->getCity($provinceid);
		$retdata = '<option value="">地级市</option>' . array2option($data, $cityid);
		if ($rettype == 'html')
		{
			exit($retdata);
		}
		else if ($rettype == 'json')
		{
			$ret['code'] = '1';
			$ret['msg'] = $retdata;
			exit(json_encode($ret));
		}
		else if ($rettype == 'return')
		{
			return $retdata;
		}
	}
	
	// 获取地区数据

	public function getRegion($cityid, $regionid = '', $rettype = 'html')
	{
		if (empty($cityid)) return '<option value="">区、县级市</option>';

		$retdata = array();
		$data = $this->region->getRegion($cityid);
		$retdata = '<option value="">区、县级市</option>' . array2option($data, $regionid);
		if ($rettype == 'html')
		{
			exit($retdata);
		}
		else if ($rettype == 'json')
		{
			$ret['code'] = '1';
			$ret['msg'] = $retdata;
			exit(json_encode($ret));
		}
		else if ($rettype == 'return')
		{
			return $retdata;
		}
	}
}
